package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.business.VO.AreaVO;
import com.ruoyi.business.VO.CustomVO;
import com.ruoyi.business.domain.BizCustomType;
import com.ruoyi.business.domain.BuSharing;
import com.ruoyi.business.domain.ScArea;
import com.ruoyi.business.service.IBizCustomTypeService;
import com.ruoyi.business.service.IBuSharingService;
import com.ruoyi.business.service.IScAreaService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BuCustomMapper;
import com.ruoyi.business.domain.BuCustom;
import com.ruoyi.business.service.IBuCustomService;

/**
 * 客户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-28
 */
@Service
public class BuCustomServiceImpl implements IBuCustomService
{
    @Autowired
    private BuCustomMapper buCustomMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private SysUserPostMapper userPostMapper;
    @Autowired
    private IScAreaService scAreaService;
    @Autowired
    private IBizCustomTypeService bizCustomTypeService;
    @Autowired
    private IBuSharingService buSharingService;
    /**
     * 查询客户信息
     *
     * @param customId 客户信息主键
     * @return 客户信息
     */
    @Override
    public BuCustom selectBuCustomByCustomId(Long customId)
    {
        return buCustomMapper.selectBuCustomByCustomId(customId);
    }

    /**
     * 查询客户信息列表
     *
     * @param buCustom 客户信息
     * @return 客户信息
     */
    @Override
    public List<BuCustom> selectBuCustomList(BuCustom buCustom)
    {
        List<BuCustom> buCustomList = buCustomMapper.selectBuCustomList(buCustom);
        /*for (BuCustom buCustom1 : buCustomList){
            SysDept sysDept = deptService.selectDeptByUserId( buCustom1.getUserId() );
            if (sysDept.getLevel()==4){
                buCustom1.setBindDeptName( sysDept.getParentName() );
            }else {
                buCustom1.setBindDeptName( sysDept.getDeptName() );
            }
        }*/
        return buCustomList;
    }

    /**
     * 新增客户信息
     *
     * @param buCustom 客户信息
     * @return 结果
     */
    @Override
    public int insertBuCustom(BuCustom buCustom)
    {
        ScArea scArea = scAreaService.selectScAreaById(buCustom.getProvinceId());
        buCustom.setProvince( scArea.getName() );
        ScArea scArea2 = scAreaService.selectScAreaById(buCustom.getCityId());
        buCustom.setCity( scArea2.getName() );
//        SysUser sysUser = userService.selectUserById( buCustom.getBindUserId() );
        buCustom.setCreateTime(DateUtils.getNowDate());
        SysUser user = new SysUser();
//        user.setDeptId( sysUser.getDeptId() );
        user.setUserName( buCustom.getPhoneNumber() );
        user.setNickName( buCustom.getCustomName() );
        user.setPassword( SecurityUtils.encryptPassword("123456") );
        user.setPhonenumber( buCustom.getPhoneNumber() );
        user.setUserType( "11" );
        user.setPostName( "客户" );
        userService.insertUser( user );
        List<SysUserPost> userPostList = new ArrayList<>();
        SysUserPost sysUserPost = new SysUserPost();
        sysUserPost.setUserId(user.getUserId());
        sysUserPost.setPostId(4L);
        userPostList.add( sysUserPost );
        userPostMapper.batchUserPost(userPostList);
        buCustom.setUserId( user.getUserId() );
        buCustomMapper.insertBuCustom(buCustom);
        List<BuSharing> buSharingList = buCustom.getBuSharingList();
        for (BuSharing buSharing : buSharingList){
            buSharing.setCustomId( buCustom.getCustomId() );
            if (buSharing.getProfitSharing()==null){
                buSharing.setProfitSharing( 0 );
            }
            buSharingService.insertBuSharing(buSharing);
        }
        return buCustomMapper.updateBuCustom(buCustom);
    }

    /**
     * 修改客户信息
     *
     * @param buCustom 客户信息
     * @return 结果
     */
    @Override
    public int updateBuCustom(BuCustom buCustom)
    {
        ScArea scArea = scAreaService.selectScAreaById(buCustom.getProvinceId());
        buCustom.setProvince( scArea.getName() );
        ScArea scArea2 = scAreaService.selectScAreaById(buCustom.getCityId());
        buCustom.setCity( scArea2.getName() );
        BuCustom buCustom2 = buCustomMapper.selectBuCustomByCustomId(buCustom.getCustomId());
        SysUser sysUser = userService.selectUserById( buCustom.getUserId() );
        sysUser.setNickName( buCustom.getCustomName() );
        sysUser.setPhonenumber( buCustom.getPhoneNumber() );
        if (!sysUser.getUserName().equals( buCustom.getPhoneNumber() )) {
            sysUser.setUserName( buCustom.getPhoneNumber() );
            //检查该账号是否重复
            if (!userService.checkUserNameUnique(sysUser))
            {
                throw new RuntimeException("手机号重复了,如需继续添加，请联系管理员处理");
            }
        }
        if (buCustom2.getBindUserId()!=buCustom.getBindUserId()){
            //重新绑定
            SysUser user = userService.selectUserById( buCustom.getBindUserId() );
            sysUser.setDeptId( user.getDeptId() );
        }
        userService.updateUser2( sysUser );
        buCustom.setUpdateTime(DateUtils.getNowDate());

        List<BuSharing> buSharingList = buCustom.getBuSharingList();
        for (BuSharing buSharing : buSharingList){
            buSharing.setCustomId( buCustom.getCustomId() );
            if (buSharing.getProfitSharing()==null){
                buSharing.setProfitSharing( 0 );
            }
            buSharingService.insertBuSharing(buSharing);
        }
        return buCustomMapper.updateBuCustom(buCustom);
    }

    /**
     * 批量删除客户信息
     *
     * @param customIds 需要删除的客户信息主键
     * @return 结果
     */
    @Override
    public int deleteBuCustomByCustomIds(Long[] customIds)
    {
        for (Long customId : customIds) {
            BuCustom buCustom = buCustomMapper.selectBuCustomByCustomId(customId);
            SysUser sysUser = userService.selectUserById( buCustom.getUserId() );
            userService.deleteUserById( sysUser.getUserId());
        }
        return buCustomMapper.deleteBuCustomByCustomIds(customIds);
    }

    /**
     * 删除客户信息信息
     *
     * @param customId 客户信息主键
     * @return 结果
     */
    @Override
    public int deleteBuCustomByCustomId(Long customId)
    {
        BuCustom buCustom = buCustomMapper.selectBuCustomByCustomId(customId);
        SysUser sysUser = userService.selectUserById( buCustom.getUserId() );
        userService.deleteUserById( sysUser.getUserId());
        return buCustomMapper.deleteBuCustomByCustomId(customId);
    }

    @Override
    public List<CustomVO> custom(){
        List<CustomVO> customVOList = new ArrayList<>();
        BizCustomType bizCustomType = new BizCustomType();
        List<BizCustomType> bizCustomTypeList = bizCustomTypeService.selectBizCustomTypeList(bizCustomType);
        for (BizCustomType bizCustomType1 : bizCustomTypeList){
            CustomVO customVO = buCustomMapper.custom(bizCustomType1.getCustomType());
            customVO.setCustomType( bizCustomType1.getCustomType() );
            if (customVO.getCustomNum()==null){
                customVO.setCustomNum( 0 );
            }
            customVOList.add( customVO );
        }
        return customVOList;
    }

    @Override
    public List<AreaVO> area(){
        List<AreaVO> areaVOList = buCustomMapper.aera();
        return areaVOList;
    }
}
